package com.hgd.evp.common.context;

import com.hgd.evp.common.constances.CrtAndUpdFieldConstances;
import com.hgd.evp.common.utils.EmptyUtil;

import java.util.HashMap;
import java.util.Map;

public class CurrentContext {
    private static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

    private static void initThreadLocalMap(){
        if(EmptyUtil.isEmpty(threadLocal.get())){
            threadLocal.set(new HashMap<>());
        }
    }

    public CurrentContext() {
    }

    public static void set(Map<String, Object> map) {
        threadLocal.set(map);
    }

    public static Map<String, Object> get() {
        return threadLocal.get();
    }

    public static void setValue(String key, Object value) {
        initThreadLocalMap();
        threadLocal.get().put(key, value);
    }

    public static Object getValue(String key) {
        initThreadLocalMap();
        return threadLocal.get().get(key);
    }

    public static String getUserId() {
        return String.valueOf(getValue(CrtAndUpdFieldConstances.USER_ID));
    }

    public static String getUserName() {
        return String.valueOf(getValue(CrtAndUpdFieldConstances.USER_NAME));
    }

    public static String getEAccount() {
        return String.valueOf(getValue(CrtAndUpdFieldConstances.E_ACCOUNT));
    }

    public static String getTenantId() {
        return String.valueOf(getValue(CrtAndUpdFieldConstances.TENANT_ID));
    }
}
